/**
  ******************************************************************************
  * @file    main.c
  * @author  Iron
  * @date    2021-05-20
  * @version v1.0
  * @brief   main c file
  */

/* Private includes ----------------------------------------------------------*/
#include <stdlib.h>
#include "shell.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

#define SHELL_BUF_SIZE              128

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/* serial helle omessage */
static const char* wellcom =
SHELL_NEW_LINE
"Mini Terminal Shell For MCUs" SHELL_NEW_LINE
"Copyright (c) 2021, Author: xwiron, Email: xwiron@aliyun.com" SHELL_NEW_LINE
"Type \"help\" to view a list of registered commands" SHELL_NEW_LINE
SHELL_NEW_LINE;

/* serial shell context */
static shell_context_t       shell_context;
struct shell_command_sets_t  shell_command_sets;
struct shell_history_queue_t shell_history;

/* Private function prototypes -----------------------------------------------*/

int main(void)
{
    shell_context_t* sh = &shell_context;

    sh->shell_name = "shell-serial";

    sh->shell_getchar = getchar;
    sh->shell_putchar = putchar;
    sh->shell_puts = puts;
    sh->shell_printf = printf;
    sh->shell_malloc = malloc;
    sh->shell_free = free;

    shell_init(sh, &shell_command_sets, &shell_history);

    sh->shell_puts(SHELL_NEW_LINE "Shell init done, Press 'Enter' to continue..." SHELL_NEW_LINE);

    while(sh->shell_getchar() != '\n');

    sh->shell_puts(wellcom);

    shell_output_prompt(sh);

    for (;;)
    {
        int data = sh->shell_getchar();

        if (data != EOF)
        {
            char *buf = sh->shell_malloc(SHELL_BUF_SIZE + 1); // +1 '\0'

            if (buf != NULL)
            {
                buf[0] = data;
                buf[1] = '\0';

                shell_getline(sh, buf, SHELL_BUF_SIZE);

                shell_input(sh, buf);

                sh->shell_free(buf);
            }
            else
            {
                /* shell malloc error */
                sh->shell_printf("Shell malloc menmory error.");
            }
        }

    }
}


/**
	* @}
	*/

	/******************* (C)COPYRIGHT 2021 ***** END OF FILE *********************/
